<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ora-在线安装</title>
<style>
body{font-family:Tahoma;}
.suc{color:green;font-size:12px;}
.fail{color:Red;font-size:12px;}
.p{padding:0;margin:2px;line-height:18px;}
</style>
</head>
<body>
<?php
if(file_exists("./public/.lock")){
	alert('alert("程序已经安装过了!如想重新安装,请删除 public 目录下的.lock文件!");');
}
function alert($msg){
	echo("<script>");
	echo($msg);
	echo("</script>");
	exit();
}
if(isset($_POST['submit'])){
	$link = @mysql_connect($_POST["host"], $_POST["uname"],$_POST["passwd"]);
	if(!@mysql_select_db($_POST['db_name'],$link)){
		//alert("alert('未能连接到指定的数据库!请确认数据库名称,帐号,密码正确')");
		if(!@mysql_query("create database `{$_POST['db_name']}`",$link)){
			alert("alert('数据库不存在(自动创建失败)! 请确认数据库名称,帐号,密码正确');history.go(-1);");
		}
	}
	@mysql_query("set names '{$_POST['charset']}'",$link);
	$sql = file_get_contents("./ora.sql");
	if(!$sql){
		alert("alert('SQL文件不存在');location.href=location.href");
	}
	$suff = str_replace('_','',$_POST['suffix'])."_";
	//执行安装
	$sql = str_replace("ora_",$suff,$sql);
	$sql = str_replace("utf8",strtolower($_POST['charset']),$sql);
	$sql = explode(";",$sql);
	for($j=0;$j<sizeof($sql);$j++){
		if($sql[$j]){
			mysql_query($sql[$j],$link);
		}
	}
	//清除Mysql错误信息,防止Ora程序因为安装产生的错误而抛出异常
	@mysql_query("set names '{$_POST['charset']}'",$link);
	//修改配置文件
	$conf = file_get_contents("./config/config.php");
	$host = $_POST['host'];
	$host = preg_replace('/http\:\/\//i','',$host);
	$host = preg_replace('/\:3306/i','',$host);
	$conf = preg_replace('/\'db_host\'\s*=\>\s*?\'.*?\'/i',"'db_host' => '".$host."'",$conf);
	$conf = preg_replace('/\'db_user\'\s*=\>\s*?\'.*?\'/i',"'db_user' => '".preg_quote($_POST['uname'])."'",$conf);
	$conf = preg_replace('/\'db_passwd\'\s*=\>\s*?\'.*?\'/i',"'db_passwd' => '".preg_quote($_POST['passwd'])."'",$conf);
	if($_POST['charset']){
		$conf = preg_replace('/\'db_encoding\'\s*=\>\s*?\'.*?\'/i',"'db_encoding' => '".preg_quote($_POST['charset'])."'",$conf);
	}
	$conf = preg_replace('/\'db_name\'\s*=\>\s*?\'.*?\'/i',"'db_name' => '".preg_quote($_POST['db_name'])."'",$conf);
	if($_POST['suffix']){
		$conf = preg_replace('/\'file_suffix\'\s*=\>\s*?\'.*?\'/i',"'file_suffix' => '".preg_quote($suff)."'",$conf);
	}
	if(file_put_contents("./config/config.php",$conf) === 0){
		alert("alert('安装失败! index.php 文件不可写!');history.go(-1); ");
	}
	file_put_contents("./public/.lock","lan7.net");
	$s = file_get_contents("./index.php");
	$s = preg_replace('/#{1,}\s+(.*?)\s+#{1,}\s+/i','',$s);
	if(file_put_contents("./index.php",$s) === 0){
		alert("alert('安装失败! index.php 文件不可写!');history.go(-1);");
	}
	alert("alert('安装成功!为了系统安全,请手动删除安装程序!');location.href='./index.php'");
}else{

	?>
	<table align="center" style="margin:20px;width:400px;background:#444;" cellspacing="1" cellpadding="5">
	<form action="./install.php" method="post">
	<tr><th colspan=2 style="padding:5px;color:#fff;font-size:14px">在线安装 &copy; <a href="http://www.lan7.net" target="_blank" style="color:#c99">lan7.net</a></th></tr>
	<tr style="background:#fff">
		<td style="text-align:right;padding-right:10px;width:30%;font-size:13px;padding:4px;color:#333">服务器地址 :</td>
		<td style="text-align:left;font-size:13px;padding:4px;color:#333"><input name="host" value="localhost" /></td>
	</tr>
	<tr style="background:#fff">
		<td style="text-align:right;padding-right:10px;width:20%;font-size:13px;padding:4px;color:#333">数据库名称 :</td>
		<td style="text-align:left;font-size:13px;padding:4px;color:#333"><input name="db_name" /></td>
	</tr>
	<tr style="background:#fff">
		<td style="text-align:right;padding-right:10px;width:20%;font-size:13px;padding:4px;color:#333">数据库用户 :</td>
		<td style="text-align:left;font-size:13px;padding:4px;color:#333"><input name="uname" /></td>
	</tr>
	<tr style="background:#fff">
		<td style="text-align:right;padding-right:10px;width:20%;font-size:13px;padding:4px;color:#333">数据库密码 :</td>
		<td style="text-align:left;font-size:13px;padding:4px;color:#333"><input name="passwd" /></td>
	</tr>
	<tr style="background:#fff">
		<td style="text-align:right;padding-right:10px;width:20%;font-size:13px;padding:4px;color:#333">编码设置 :</td>
		<td style="text-align:left;font-size:13px;padding:4px;color:#333"><input name="charset" value="utf8" /></td>
	</tr>
	<tr style="background:#fff">
		<td style="text-align:right;padding-right:10px;width:20%;font-size:13px;padding:4px;color:#333">数据表前缀 :</td>
		<td style="text-align:left;font-size:13px;padding:4px;color:#333"><input name="suffix" value="ora_" /></td>
	</tr>
	<tr style="background:#fff">
		<th style="text-align:center;font-size:13px;padding:4px;padding-top:7px;color:#333" colspan=2>环境检查</th>
	</tr>
	<?php
	$tmp = array(
	array('cache',0,'缓存目录'),
	array('config',0,'配置目录'),
	array('public',0,'公用目录'),
	array('upload',0,'上传目录'),
	array('update',0,'更新目录'),
	array('themes',0,'模板目录'),
	array('plugin',0,'插件目录'),
	array('html',0,'Html目录'),
	array('index.php',1,'首页文件'),
	array('config/config.php',1,'配置文件')
	);
	foreach ($tmp as $k => $v){
	?>
	<tr style="background:#fff">
		<td style="text-align:right;padding-right:10px;width:20%;font-size:13px;padding:4px;color:#333"><?=$v[2]?> :</td>
		<td style="text-align:left;font-size:13px;padding:4px;color:#333">
		<?=checkfdperm("./".$v[0],$v[1])?'<span class="suc">可写</span>':'<span class="fail"> ./'.$v[0].' 不可写!</span>'?>
		</td>
	</tr>
	<?php
	}
	?>
	
	<tr style="background:#fff">
		<td style="text-align:right;padding-right:10px;width:20%;font-size:13px;padding:4px;color:#333"></td>
		<td style="text-align:left;font-size:13px;padding:4px;color:#333"><input type="submit" name="submit"  value="开始安装"/></td>
	</tr>
	</form>
	</table>
	<?php

}
function checkfdperm($path, $isfile=0) {
	if($isfile) {
		$file = $path;
		$mod = 'a';
	} else {
		$file = $path.'/install_tmptest.data';
		$mod = 'w';
	}
	if(!$fp = @fopen($file, $mod)) {
		return false;
	}
	if(!$isfile) {
		//是否可以删除
		fwrite($fp, ' ');
		fclose($fp);
		if(!@unlink($file)) {
			return false;
		}
		//检测是否可以创建子目录
		if(is_dir($path.'/install_tmpdir')) {
			if(!@rmdir($path.'/install_tmpdir')) {
				return false;
			}
		}
		if(!@mkdir($path.'/install_tmpdir')) {
			return false;
		}
		//是否可以删除
		if(!@rmdir($path.'/install_tmpdir')) {
			return false;
		}
	} else {
		fclose($fp);
	}
	return true;
}

?>
</body>
</html>